package client

import (
	"strings"
	"yunj/app/enum/adenum"
	service "yunj/app/service/client"
	"yunj/pkg/util"
	yunjUtil "yunj/pkg/yunj/util"

	"github.com/gin-gonic/gin"
)

type Article struct {
	Controller
}

// 文章详情
func (ctrl *Article) Detail(c *gin.Context) {
	// 接收参数。兼容.html后缀格式
	idStr := yunjUtil.PathParam(c, "id")
	idStr = strings.TrimSuffix(idStr, ".html")
	id, err := yunjUtil.ToUint64(idStr)
	if err != nil || id <= 0 {
		util.RedirectError(c, "异常文章数据访问！")
		return
	}
	// 获取文章数据
	article, err := service.Article.Detail(id)
	if err != nil {
		util.RedirectError(c, err.Error())
		return
	}
	// 组装渲染数据
	data := map[string]interface{}{
		"article":     article,
		"detailTopAd": service.Ad.ItemsByNormalPublish(adenum.LOCATION_ARTICLE_DETAIL_TOP),
	}
	ctrl.Render(c, "article/detail", data)
}
